<?php

class Default_IndexController extends ZF_DefaultController {

    public function successAction() {
        $_displayname = $this->getRequest()->getParam('displayname');

        $this->view->displayname = $_displayname;
    }

    public function errorAction() {
        
    }

    public function indexAction() {
        
    }

    public function loginAction() {
        $username = $this->getRequest()->getParam('username');
        $password = $this->getRequest()->getParam('password');
        $auth = Zend_Auth::getInstance();
        $db = Zend_Registry::get("db");
        $authAdapter = new Zend_Auth_Adapter_DbTable($db, "user", "username", "pw");


        $authAdapter->setIdentity($username);
        $authAdapter->setCredential(md5($password));

        $result = $auth->authenticate($authAdapter);

        if ($result->isValid()) {
            $data_auth = $authAdapter->getResultRowObject(null, array('pw'));
            $auth->getStorage()->write($data_auth);
            $this->view->profileNameSpace = (array) $data_auth;
            $data = array('error' => '0');
            $this->_helper->json->sendJson($data);
            
        }  else {
                $data = array('error' => '1');
                $this->_helper->json->sendJson($data);
        }
        $this->_redirect("/index");
    }

    public function logoutAction() {
        $profileNameSpace = new Zend_Session_Namespace('profile');
        $profileNameSpace->user = null;
        
        Zend_Auth::getInstance()->clearIdentity();
        $this->_redirect("/user/login");
    }

    public function registerAction() {
        $this->view->header = "Register";
        $Common = new Default_Model_Common();
        $this->view->city = $Common->CITY;
        if ($this->getRequest()->isPost()) {
            $_username = $this->getRequest()->getParam('username');
            $_pw = $this->getRequest()->getParam('password');
            $_fullname = $this->getRequest()->getParam('fullname');
            $_displayname = $this->getRequest()->getParam('displayName');
            $_telephone = $this->getRequest()->getParam('phone');
            $_email = $this->getRequest()->getParam('email');
            $_address = $this->getRequest()->getParam('address');
            $_city = $this->getRequest()->getParam('city');
            if ($_address && $_displayname && $_email && $_fullname && $_pw && $_telephone && $_username) {
                $model_user = new Default_Model_User();
                if ($model_user->insert($_username, $_pw, $_fullname, $_displayname, $_telephone, $_email, $_address, $_city) != -1) {
                    $user = $model_user->getByUsername($_username);
                    $profileNameSpace = new Zend_Session_Namespace('profile');
                    $profileNameSpace->user = $user;
                    $this->redirect("/index/success/displayname/" . $_displayname);
                } else {
                    $this->redirect("/index/error");
                }
            } else {
                $this->redirect("/index/error");
            }
        }
        return;
    }

}
